package com.spark.laboratory.service.impl;

import com.spark.laboratory.mapper.UsersMapper;
import com.spark.laboratory.pojo.Users;
import com.spark.laboratory.pojo.UsersExample;
import com.spark.laboratory.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    private final UsersMapper usersMapper;

    @Autowired
    public UsersServiceImpl(UsersMapper usersMapper) {
        this.usersMapper = usersMapper;
    }

    @Override
    public Users login(String userSchool, String userStudentNumber) {

        UsersExample usersExample = new UsersExample();
        // 去除重复，boolean型，true为选择不重复的记录。
        usersExample.setDistinct(true);
        UsersExample.Criteria criteria = new UsersExample().createCriteria();
        criteria.andUserSchoolEqualTo(userSchool);
        criteria.andUserStudentNumberEqualTo(userStudentNumber);
        List<Users> users = usersMapper.selectByExample(usersExample);
        System.out.println(users);
        if (users.size() >= 1){
            return users.get(0);
        }else {
            return null;
        }
    }

    /**
     * 查询在校学生是否存在
     * @param userSchool 用户学校
     * @param userStudentNumber 用户学号
     * @return
     */
    @Override
    public Users queryUserIsExist_1(String userSchool, String userStudentNumber, String userName) {
        return usersMapper.selectUsers_1(userSchool, userStudentNumber, userName);
    }

    /**
     * 查询非在校生是否存在
     * @param userSchool 用户学校
     * @param userName 用户姓名
     * @return true 表示存在 false 表示不存在
     */
    @Override
    public Users queryUserIsExist_2(String userSchool, String userName) {
        return usersMapper.selectUsers_2(userSchool == null ? "" : userSchool,
                userName == null ? "" : userName);
    }

    @Override
    public void saveUser(Users user) {
        usersMapper.insertSelective(user);
    }

    @Override
    public void saveExpertScore(Integer value) {
    }

    @Override
    public void saveStudentScore(Integer userId, String value) {
        Users users = new Users();
        users.setId(userId);
        users.setUserScore(value);
        usersMapper.updateByPrimaryKeySelective(users);
    }

    @Override
    public List<Users> getAllData() {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andIsDeleteEqualTo(0);
        return usersMapper.selectByExample(usersExample);
    }

    @Override
    public Users getUserById(Integer userId) {
        return usersMapper.selectByPrimaryKey(userId);
    }

    @Override
    public Integer getHasScoreUser() {
        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria criteria = usersExample.createCriteria();
        criteria.andUserScoreIsNotNull();
        List<Users> users = usersMapper.selectByExample(usersExample);
        return users != null? users.size() : 0;
    }

    @Override
    public Double getPassingRate() {
        // 通过率 = 成绩在(满分)140*0.6以上人数 / 有成绩人数
        // 成绩在(满分)140*0.6以上人数
        Integer passScore = 84;
        Integer i = usersMapper.selectUserScoreGreaterThan(passScore);
        Integer hasScoreUser = getHasScoreUser();

        System.out.println("hasScoreUser = " + hasScoreUser);
        System.out.println("i = " + i);

        if (hasScoreUser != 0){
            return new BigDecimal((float)i / hasScoreUser).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }

        return 0.0;
    }

    @Override
    public Users queryUser(String userName) {
        return usersMapper.selectByUserName(userName);
    }
}
